package com.mate.cloud.controller;

import com.mate.cloud.domain.User;
import com.mate.cloud.service.UserService;
import com.mate.cloud.cache.annotation.RepeatSubmit;
import com.mate.cloud.common.response.BaseResponse;
import com.mate.cloud.common.web.controller.AdminBaseController;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;

/**
 * 控制器
 *
 * @author cong zhi
 * @email 448341911@qq.com
 * @createDate 2023/2/24 15:27
 * @version: v1.0
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController extends AdminBaseController {

    @Resource
    private UserService userService;

    /**
     * 默认1s，方便测试查看，写10s
     *
     * @param user
     * @return
     */
    @PostMapping("/add")
    @RepeatSubmit(expireTime = 10)
    public BaseResponse add(@RequestBody User user) {
        log.info("========UserController#add========:{}", JSON.toJSONString(user));
        boolean result = userService.save(user);
        if (result) {
            return successMsgResponse("添加成功！");
        } else {
            return successMsgResponse("添加失败!");
        }
    }


    @GetMapping("/del/{id}")
    public BaseResponse del(@PathVariable("id") Integer id) {

        boolean result = userService.removeById(id);
        if (result) {
            return successMsgResponse("删除成功！");
        } else {
            return successMsgResponse("删除失败!");
        }
    }

    @GetMapping("/load/{id}")
    public BaseResponse load(@PathVariable("id") Integer id) {
        log.info("========UserController#load========:{}", id);
        return successBodyResponse(userService.getById(id));
    }

    @GetMapping("/list")
    public BaseResponse list() {
        return successBodyResponse(userService.list());
    }

}
